package top.wangjiu.reactive.rpc.handler;

import top.wangjiu.reactive.rpc.model.RpcRequest;
import top.wangjiu.reactive.rpc.model.RpcResponse;

/**
 * @ClassName DefaultResultTypeHandler
 * @Description TODO
 * @Author wangjiu
 * @Date 2021/8/3 5:05 下午
 */
public class DefaultRequestHandler extends AbstractRequestHandler<Object> {

    private volatile String requestId;

    private volatile Object res;

    @Override
    public void onRequest(RpcRequest request) {
        if(requestId == null){
            requestId = request.getRequestId();
            RpcResponse response = new RpcResponse(requestId);
            response.setFirst(true);
            response.setLast(true);
            try {
                res = invoke(request);
                response.setResult(res);
            } catch (Exception e) {
                response.setSuccess(false);
                response.setCause(e);
            }
            write(response);
        }else{

        }
    }

    @Override
    public void onDisconnect() {

    }
}
